import store from '../store'
import router from '../router'

export function publicKey() {
  return 'MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQCwjDm1HXDw8QH5ZtGMQIl2h/I8E+chOQA8aQ8xCR/+aHnROaN/ZU5Vmd2Zz7g6cAacR9BSm60+iSCYtvEGJKl0WqvbPGJkc8tedjNF1QqgWqkkuE6Udgw2OkEKJCxDg6PrAniR7Cc0io9G8bW4P8JDJjSbbafvMPDDFbVVUWJxxwIDAQAB'
}
/**
 * 数据保存到sessionStorage
 * @param key
 * @param value
 */
export function setSession(key, value){
  // console.log('key=', key)
  // console.log('value=', value)
  if (value === undefined || key === undefined) {
    return
  }
  if (typeof value === 'number' || typeof value === 'string') {
    localStorage[`${key}`] = value
  } else if (typeof value === 'object' && value !== null) {
    localStorage[`${key}`] = JSON.stringify(value)
  }
}

/**
 * 获取sessionStorage数据
 * @param key
 * @param key
 */
export function getSession(key) {
  if (!localStorage[`${key}`]) {
    return
  }
  var value = localStorage[`${key}`]
  var index1 = value.indexOf('[')
  var index2 = value.indexOf('{')
  if (index1 >= 0 || (index2 >= 0)) {
    value = JSON.parse(localStorage[`${key}`])
  }
  return value
}

/**
 * 省市区搜索下拉列表
 * @param data
 */
export function transformSearchArea(data){
  var insertPart = {
    data: '全部',
    id: ''
  }
  data.unshift(insertPart)
  for (var i = 0; i < data.length; i++) {
    var province = data[i]
    province.label = province.data
    province.value = province.id
    if (province.children && province.children.length) {
      province.children.unshift(insertPart)
      for (var j = 0; j < province.children.length; j++) {
        var city = province.children[j]
        city.label = city.data
        city.value = city.id
        if (city.children && city.children.length) {
          city.children.unshift(insertPart)
          for (var k = 0; k < city.children.length; k++) {
            var area = city.children[k]
            area.label = area.data
            area.value = area.id
          }
        }
      }
    }
  }
  return data
}
/**
 * 省市区非搜索下拉列表
 * @param data
 */
export function transformSelectArea(data) {
  for (let i = 0; i < data.length; i++) {
    let province = data[i]
    province.label = province.data
    province.value = province.id
    if (province.children && province.children.length) {
      for (let j = 0; j < province.children.length; j++) {
        let city = province.children[j]
        city.label = city.data
        city.value = city.id
        if (city.children && city.children.length) {
          for (let k = 0; k < city.children.length; k++) {
            let area = city.children[k]
            area.label = area.data
            area.value = area.id
          }
        }
      }
    }
  }
  return data
}

/**
 * 清空搜索条件
 * @param selectData
 * @param searchParam
 * @param vm
 */
export function clearSelect(selectData, searchParam, vm) {
  if (selectData && Object.keys(selectData).length) {
    for (let item in selectData) {
      if (Array.isArray(selectData[item])) {
        selectData[item] = []
      } else {
        selectData[item] = ''
      }
    }
  }
  if (searchParam && Object.keys(searchParam).length) {
    for (let ele in searchParam) {
      if (Array.isArray(searchParam[ele])) {
        searchParam[ele] = []
      } else {
        if (ele !== 'currentPage' && ele !== 'pageSize' && ele !== 'warningLabel') {
          searchParam[ele] = ''
        }
      }
    }
  }
  vm.timeTitle = '全部时间'
}
/**
 * 清空搜索条件,搜索条件的key和更多选择中的key值一致，则清空时，将两个对象对应的值都清空
 * @param selectData
 * @param searchParam
 * @param vm
 */
export function clearMoreSelect (moreSelectData, searchParam, vm) {
  if (moreSelectData && Object.keys(moreSelectData).length) {
    for (let item in moreSelectData) {
      if (Array.isArray(moreSelectData[item])) {
        moreSelectData[item] = []
        searchParam[item] = []
      } else {
        moreSelectData[item] = ''
        searchParam[item] = ''
      }
    }
  }
}
/**
 * 设备类型转换为中文
 * @param val
 * @returns {*}
 */
export function devTypeTransfrom (val) {
  let devTypeMapDict = getSession('devTypeDict')
  if (devTypeMapDict[val]) {
    return devTypeMapDict[val]
  } else {
    return val
  }
}

/**
 *  省市区的数据过滤
 * @param param
 */
export function transformArea(param){
  // data对象省市区的字段名必须为（areaCode）
  let data = JSON.parse(JSON.stringify(param));
  if (data.areaCode) {
    data.areaCode = data.areaCode[2] || data.areaCode[1] || data.areaCode[0];
  }
  // brandModel品牌型号参数拆分
  if (data.brandModel && data.brandModel.length === 2) {
    data.brand = data.brandModel[0].slice(2);
    data.model = data.brandModel[1].slice(2);
  }
  return data;
}

/**
 *  关闭当前页并返回上页（或指定页）
 * @param oldUrl
 * @param newUrl
 */
export function closeSelectedTag(oldUrl, newUrl) {
  store.dispatch('tagsView/delView', oldUrl).then(() => {
    newUrl ? router.push(newUrl) :  router.go(-1);
  })
}